package main

import (
	"bytes"
	"encoding/json"
	"fmt"
	"io/ioutil"
	"net/http"

	"github.com/ping40/miscgo/parsehtml/util"
)

func main() {
	k := &JsonPostSample{}
	k.SamplePost()
}

type JsonPostSample struct {
}

func (this *JsonPostSample) SamplePost() {

	reader := bytes.NewReader([]byte("query=%7B%22c%22%3A10%2C%22st%22%3A%220%22%2C%22f%22%3A%5B%5D%2C%22p%22%3A%22%22%2C%22q%22%3A%5B%7B%22k%22%3A%22%22%2C%22v%22%3A%22%E4%BA%AC%E6%B4%A5%E5%86%80%22%2C%22e%22%3A1%2C%22es%22%3A%7B%7D%2C%22o%22%3A%22AND%22%2C%22a%22%3A0%7D%5D%2C%22op%22%3A%22AND%22%2C%22s%22%3A%5Bnull%5D%2C%22t%22%3A%5B%22Report%22%5D%7D&webDisplayId=11&sl=1&searchWordId=028084d23d37f0f1f93c9053a89ab1d6&searchId=27189cef0901c1638711c356a5f289e5&facetRelation=%5B%5D&pageSize=10&pageNumber=1"))
	url := "https://www.nstl.gov.cn/execute?target=nstl4.search4&function=paper/pc/list/pl"
	request, err := http.NewRequest("POST", url, reader)
	if err != nil {
		fmt.Println(err.Error())
		return
	}
	request.Header.Set("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8")

	// cookie1 := &http.Cookie{Name: "NCK", Value: "YKSAREGSQ5YZLYDFHC5DVSSIY23Y3VAK"}
	// request.AddCookie(cookie1)

	// cookie1 = &http.Cookie{Name: "libcode", Value: "CN000000"}
	// request.AddCookie(cookie1)

	// cookie1 = &http.Cookie{Name: "C", Value: "0"}
	// request.AddCookie(cookie1)

	// cookie1 = &http.Cookie{Name: "JSESSIONID", Value: "C082E43E83A7EFB65F117A7926C76850"}
	// request.AddCookie(cookie1)

	// cookie1 = &http.Cookie{Name: "TS01d48c5b", Value: "01aa0dedf26ace3637785c924c160d99bd0a2a414e3d3505bbf1a5c4e3e06019ba0ec9a7b2bfb17a253bdddce65fb7306ea4993fe8"}
	// request.AddCookie(cookie1)

	// cookie1 = &http.Cookie{Name: "TS016032ff", Value: "01aa0dedf29581429d73b07ee4864ffa890752a545e73f3a7589ea9553a697572cd698db02f6928a3fffe27ce45a6cae46b4227e80"}
	// request.AddCookie(cookie1)

	// request.Header.Add("Origin", "https://www.nstl.gov.cn")
	// request.Header.Add("Referer", "https://www.nstl.gov.cn/search.html?t=Report&q=5Lqs5rSl5YaA")
	request.Header.Add("X-Requested-With", "XMLHttpRequest")
	request.Header.Add("Accept", "application/json, text/javascript, */*; q=0.01")

	client := http.Client{}
	resp, err := client.Do(request)
	if err != nil {
		fmt.Println(err.Error())
		return
	}
	respBytes, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		fmt.Println(err.Error())
		return
	}

	results := util.Transfer(string(respBytes))

	allstudentjson, _ := json.Marshal(results)
	fmt.Println(string(allstudentjson))

	for _, item := range results {
		url := makeUrl(item.Issue[0], item.Id, item.Title)
		if err := util.DownloadFileFromUrl(url, makeFilename(item.Title, item.Id), "E:\\tmp\\file"); err != nil {
			panic(err)
		}
		break
	}

}

func makeUrl(subType, id, title string) string {
	return fmt.Sprintf("https://www.nstl.gov.cn/execute/download?target=nstl4.download&function=download&subType=%s&id=%s&title=%s",
		subType, id, title)
}

func makeFilename(title, id string) string {
	return fmt.Sprintf("%s%s.pdf", title, id)
}
